package com.mall.shop.mapper;

import com.mall.shop.entity.CompanyEntity;
import com.mall.shop.vo.CityCompanyVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CompanyEntityMapper {

    @Select({"SELECT * FROM `people`.`company` where id = #{id,jdbcType=INTEGER} LIMIT 1"})
    // 映射字段，主要是解决数据库字段与实体字段不一致
    @Results(id="CompanyEntity", value = {
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="city_code", property="city_code", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="company", property="company", jdbcType=JdbcType.VARCHAR),
            @Result(column="is_company", property="is_company", jdbcType=JdbcType.INTEGER),
            @Result(column="contact_person", property="contact_person", jdbcType=JdbcType.VARCHAR),
            @Result(column="member", property="member", jdbcType=JdbcType.INTEGER),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column = "created_time", property = "created_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_time", property = "updated_time", jdbcType = JdbcType.TIMESTAMP),
    })

    CompanyEntity selectByPrimaryKey(Integer id);


    // 学会多层嵌套查询子集
    @Select({"SELECT * FROM `people`.`company`  LIMIT 10"})

    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="city_code", property="city_code", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="company", property="company", jdbcType=JdbcType.VARCHAR),
            @Result(column="is_company", property="is_company", jdbcType=JdbcType.INTEGER),
            @Result(column="contact_person", property="contact_person", jdbcType=JdbcType.VARCHAR),
            @Result(column="member", property="member", jdbcType=JdbcType.INTEGER),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column = "id", property = "service_list", javaType = List.class, many = @Many(select = "com.mall.shop.mapper.ServicesEntityMapper.selectServicesPrimaryKey") ),
            @Result(column = "created_time", property = "created_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_time", property = "updated_time", jdbcType = JdbcType.TIMESTAMP),
    })
    List<CompanyEntity> selectAll();

    // 查询公司所在的地区
    @Select({"SELECT " +
            " a.id, " +
            " c.`name` AS province, " +
            " b.`name` AS city, " +
            " a.company, " +
            " a.city_code," +
            " a.is_company, " +
            " a.contact_person, " +
            " a.member, " +
            " a.phone, " +
            " a.created_time, " +
            " a.updated_time  " +
            "FROM " +
            " company AS a " +
            " INNER JOIN city AS b ON a.city_code = b.`code` " +
            " INNER JOIN city AS c ON b.parent_id = c.id LIMIT 50"})

    List<CityCompanyVo> selectCityShop();

    // inset 获取自增id
    @Insert({" INSERT INTO " +
            " `company`" +
            " (`city_code`, `company`, `contact_person`, `member`, `phone`) " +
            " VALUES " +
            " (#{city_code,jdbcType=INTEGER}, #{company,jdbcType=VARCHAR}, #{contact_person,jdbcType=VARCHAR}, #{member,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR})"})
    // 获取自增ID
    @Options(useGeneratedKeys = true, keyProperty = "id")
    // 返回数据库影响的条数
    int insertCompany(CompanyEntity company);
}
